﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Zx.JsonV.ToTree
{
    public class Convert
    {
        public static string ArrayHeaderFormat = "[{0}]";
        public static string PropertyHeaderFormat = "{0}:{1}";
        public static string ValueHeaderFormat = "{0}";
        public static string GetTree(JToken jsonObject, TreeViewItem treeNode)
        {
            if (treeNode == null)
                treeNode = new TreeViewItem();
            if (jsonObject == null)
            {
                return "null";
            }
            string v="";
            switch (jsonObject.Type)
            {
                case JTokenType.Array:
                    v= GetTree((JArray)jsonObject, treeNode);
                    break;
                case JTokenType.Boolean:
                case JTokenType.Bytes:
                case JTokenType.Date:
                case JTokenType.Float:
                case JTokenType.Integer:
                case JTokenType.String:
                case JTokenType.None:
                case JTokenType.Undefined:
                case JTokenType.Null:
                case JTokenType.Comment:
                    v= GetTree((JValue)jsonObject, treeNode);
                    break;
                case JTokenType.Constructor:
                    break;
                case JTokenType.Object:
                    v= GetTree((JObject)jsonObject,treeNode);
                    break;
                case JTokenType.Property:
                    v= GetTree((JProperty)jsonObject,treeNode);
                    break;
                case JTokenType.Raw:
                    break;
                default:
                    break;
            }
            v = v + ((jsonObject.Next == null) ? "" : ",");
            treeNode.Header = v;
            treeNode.Tag = jsonObject;
            return v;
        }

        public static string GetTree(JArray jarray, TreeViewItem treeNode)
        {
            foreach (var item in jarray)
            {
                var treeNodeitem=new TreeViewItem();
                treeNodeitem.Header = GetTree(item, treeNodeitem);
                treeNode.Items.Add(treeNodeitem);
            }
            return string.Format(ArrayHeaderFormat, jarray.Count);
        }
        public static string GetTree(JObject jobject, TreeViewItem treeNode)
        {
            foreach (var item in jobject.Properties())
            {
                var treeNodeitem = new TreeViewItem();
                treeNodeitem.Header = GetTree(item, treeNodeitem);
                treeNode.Items.Add(treeNodeitem);
            }
            var s = JsonConvert.SerializeObject(jobject);
            return s.Length>10?s.Remove(9)+"..}":s;
        }
        public static string GetTree(JProperty jproperty, TreeViewItem treeNode)
        {
            var v= GetTree(jproperty.Value,treeNode);
            return string.Format(PropertyHeaderFormat, jproperty.Name, v);
        }
        public static string GetTree(JValue jvalue, TreeViewItem treeNode)
        {
            return string.Format(ValueHeaderFormat, jvalue); ;
        }
    }
}
